package com.example.test.exam;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.test.R;
import com.example.test.all_Myhelper.exam_Myhelper;
import com.example.test.all_Myhelper.test_Myhelper;
import com.example.test.login.login_main_activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class Main extends AppCompatActivity  {
    exam_Myhelper myHelper;
    test_Myhelper testMyhelper;
    public ListView mListView;
    public TextView item_time;
    public Button btn,back;

    public String[] titles = {"测试1",//标题
            "测试2",
            "测试3"
    };

    public int[] icons= {R.drawable.english,R.drawable.english,R.drawable.english};


    Date st = new Date();
    Date start = new Date(st.getTime()+8*60*60*1000);
    Date endtime_10mins = new Date(start.getTime()+600000);//1s=1000ms加10分钟   这里加的是时区问题
    Date endtime_1h = new Date(start.getTime()+3600000);
    Date endtime_2h = new Date(start.getTime()+7200000);
    SimpleDateFormat ft = new SimpleDateFormat("HH:mm:ss");
//    public String[] times = {ft.format(start)+"~"+ft.format(endtime_10mins),
//            ft.format(start)+"~"+ft.format(endtime_1h),
//            ft.format(start)+"~"+ft.format(endtime_2h)
//    };

    public String[] times = {"测试时间：10min","测试时间：1h","测试时间：2h"};
//    Calendar calendar = Calendar.getInstance();
//    Timer timer = new Timer();
//    public void timers(){
//        timer.scheduleAtFixedRate(new TimerTask() {
//            @Override
//            public void run() {
//                 int s = calendar.get(Calendar.SECOND);
//            }
//        },0,1000);
//    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        item_time = (TextView) findViewById(R.id.item_time);
        mListView = (ListView) findViewById(R.id.lv);//list列表
        btn = (Button) findViewById(R.id.goumai2);
        back = (Button) findViewById(R.id.exam_main_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent test_add = new Intent(Main.this, login_main_activity.class);
                startActivity(test_add);
            }
        });
//        btnClickListener = new ButtonClickListener();//
        myHelper = new exam_Myhelper(this);
        testMyhelper = new test_Myhelper(this);
        MyBaseAdapter myBaseAdapter = new MyBaseAdapter();
        mListView.setAdapter(myBaseAdapter);
//        mListView.setOnItemClickListener();
        System.out.println("cccccc"+Title());
        System.out.println("id is"+test_id());
    }



    public List<String> Title(){//从数据库中取出题目集合
        int i=0;
        SQLiteDatabase db;
        db = testMyhelper.getReadableDatabase();
        Cursor cursor = db.query("test",null,null,null,null,null,null);
        String[] title = new String[cursor.getCount()];
        while (cursor.moveToNext()){
            title[i] = cursor.getString(1);
            i++;
        }
        List<String> list = Arrays.asList(title);
        cursor.close();
        db.close();
        return list;
    }

    public List<Integer> ttm(){
        SQLiteDatabase db;
        int i=0;
        db = testMyhelper.getReadableDatabase();
        Cursor cursor = db.query("test",null,null,null,null,null,null);
        Integer[] all_titles = new Integer[cursor.getCount()];
        while (cursor.moveToNext()){
            all_titles[i] = cursor.getInt(3);
            i++;
        }
        List<Integer> list = Arrays.asList(all_titles);
        cursor.close();
        db.close();
        return list;
    }

    public List<Integer> test_id(){//从数据库中取出题目集合
        int i=0;
        SQLiteDatabase db;
        db = testMyhelper.getReadableDatabase();
        Cursor cursor = db.query("test",null,null,null,null,null,null);
        Integer[] title = new Integer[cursor.getCount()];
        while (cursor.moveToNext()){
            title[i] = cursor.getInt(0);
            i++;
        }
        List<Integer> list = Arrays.asList(title);
        cursor.close();
        db.close();
        return list;
    }

  public static class ViewHolder{
        public TextView title,time;
        public ImageView icon;
        public Button bt;
    }


    class MyBaseAdapter extends BaseAdapter{
        public List<String> test_title_list = new ArrayList<>(Title());
        public List<Integer> test_time = new ArrayList<>(ttm());

        @Override
        public int getCount() {
            return test_title_list.size();
        }

        @Override
        public Object getItem(int position) {
            return test_title_list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if(convertView == null){
                convertView = View.inflate(Main.this,R.layout.list_item,null);
                holder = new ViewHolder();
                holder.title = (TextView) convertView.findViewById(R.id.item_title);
                holder.time = (TextView) convertView.findViewById(R.id.item_time);
                holder.icon = (ImageView) convertView.findViewById(R.id.iv);
                holder.bt = (Button) convertView.findViewById(R.id.goumai2);
//                holder.bt.setOnClickListener(btnClickListener);
                convertView.setTag(holder);

            }else {
                holder = (ViewHolder) convertView.getTag();
            }
                holder.title.setText(test_title_list.get(position));
                holder.time.setText("限时："+(test_time.get(position)/60)+"分钟");
                holder.icon.setBackgroundResource(icons[position]);
                holder.bt.setTag(position);
                holder.bt.setOnClickListener( new View.OnClickListener() {

                    public void onClick( View v ) {
//                        if(position==0){
//                            alertDialog(position);
//                        }else if(position==1) {
//                            alertDialog(position);
//                        }else if(position==2){
//                            alertDialog(position);
//                        }
                        alertDialog(position);
                    }

                });
                return convertView;
        }
    }
    public void alertDialog(int postion){
        AlertDialog aldg;
        AlertDialog.Builder adbd = new AlertDialog.Builder(Main.this);
        adbd.setTitle("提示");
        adbd.setMessage("提交试卷之前不可退出！");
        adbd.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent cs_intent = new Intent(Main.this,ceshi.class);
                Bundle bundle = new Bundle();
                bundle.putInt("ceshi",postion);
                bundle.putInt("id",test_id().get(postion));
                cs_intent.putExtras(bundle);
//                System.out.println("时间是"+start_time);
                startActivity(cs_intent);
            }
        });
        adbd.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        aldg = adbd.create();
        aldg.show();
    }

    public void alertDialog2(){
        AlertDialog aldg2;
        AlertDialog.Builder adbd2 = new AlertDialog.Builder(Main.this);
        adbd2.setTitle("提示");

        adbd2.setMessage("未到时间不可答题！\n"+"当前时间:"+ft.format(System.currentTimeMillis())+'\n'+"开始时间:"+"12:00:00");
        adbd2.setNegativeButton("返回", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        aldg2 = adbd2.create();
        aldg2.show();
    }
}

